import type { CreateAppFunction } from "vue";
import DialogView from "./DialogView.vue";
import type { DialogParams } from "./index.d";

interface Dependencies {
  Vue: {
    createApp: CreateAppFunction<unknown>;
  };
  ElementPlus: any; // 实际项目中可以用更精确的类型
}

// 确保Element Plus样式被引入
import "element-plus/dist/index.css";

export default function createDialog(
  {
    config,
    bizData,
    onCallback
  }: {
    config: Partial<DialogParams>;
    bizData: any;
    onCallback?: (params: any) => Promise<void>;
  },
  deps: Dependencies
) {
  console.log("封装后的createDialog1111", config, bizData, onCallback);
  const parentNode = document.createElement("div");
  document.body.appendChild(parentNode);

  const cleanup = () => {
    if (appInstance) {
      appInstance.unmount();
    }
    if (parentNode.parentNode) {
      parentNode.parentNode.removeChild(parentNode);
    }
  };

  let appInstance: any = null;

 const createAppInstance = () => {
    // 使用外部传入的 Vue.createApp
    appInstance = deps.Vue.createApp(DialogView, {
      config,
      bizData,
      onCallback,
      modelValue: true,
      onClose: cleanup,
    });

    // 使用外部传入的 ElementPlus
    appInstance.use(deps.ElementPlus);
    appInstance.mount(parentNode);
  };

  // 5. 直接创建应用，不再有任何动态 import 的逻辑
  createAppInstance();

  return cleanup; // 返回清理函数
}
